package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
/**
 * 使用指定构造器实例化对象
 */
public class ReflectDemo3 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Person p = new Person("李四",22);
        System.out.println(p);

        Class cls=Class.forName("reflect.Person");
        //        cls.getConstructor();//不传参数时,获取的就是无参构造器
        /*
            Constructor对象的每一个实例用于表示一个构造器
         */
        //Person(String,int)
        Constructor c=cls.getConstructor(String.class,int.class);
        Object obj=c.newInstance("张三",22);
        System.out.println(obj);
    }
}
